<?php

include_once '../control/conexao.php';
include_once 'submenu.class.php';

class submenuDAO {

    public function listar_por_menu($perfil, $menu) {
        $consulta = mysql_query("SELECT * FROM submenu WHERE id_menu='$menu->id_menu' AND id_submenu IN (SELECT submenu_id_submenu FROM perfil_submenu WHERE perfil_id_perfil='$perfil')");
        $ln = mysql_num_rows($consulta);
        $arSubMenu = array();
        for ($i = 0; $i < $ln; $i++) {
            $arSubMenu[$i] = new menu();
            $arSubMenu[$i]->id_submenu = mysql_result($consulta, $i, 'id_submenu');
            $arSubMenu[$i]->submenu_nome = mysql_result($consulta, $i, 'submenu_nome');
            $arSubMenu[$i]->submenu_icone = mysql_result($consulta, $i, 'submenu_icone');
            $arSubMenu[$i]->submenu_show = mysql_result($consulta, $i, 'submenu_show');
            $arSubMenu[$i]->submenu_link = mysql_result($consulta, $i, 'submenu_link');
        }
        return $arSubMenu;
    }
    
    public function listar() {

        $consulta = mysql_query("SELECT * FROM submenu ORDER BY id_menu");
        $ln = mysql_num_rows($consulta);
        $arSubMenu = array();
        for ($i = 0; $i < $ln; $i++) {
            $arSubMenu[$i] = new menu();
            $arSubMenu[$i]->id_submenu = mysql_result($consulta, $i, 'id_submenu');
            $arSubMenu[$i]->submenu_nome = mysql_result($consulta, $i, 'submenu_nome');
            $arSubMenu[$i]->submenu_icone = mysql_result($consulta, $i, 'submenu_icone');
            $arSubMenu[$i]->submenu_link = mysql_result($consulta, $i, 'submenu_link');
            $arSubMenu[$i]->submenu_show = mysql_result($consulta, $i, 'submenu_show');
            $arSubMenu[$i]->id_menu = mysql_result($consulta, $i, 'id_menu');
        }
        return $arSubMenu;
    }

    public function consultar($id) {
        $consulta = mysql_query("SELECT * FROM submenu WHERE id_submenu = '$id'");
        $submenu = new submenu();
        $submenu->id_submenu = mysql_result($consulta, 0, 'id_submenu');
        $submenu->submenu_nome = mysql_result($consulta, 0, 'submenu_nome');
        $submenu->submenu_link = mysql_result($consulta, 0, 'submenu_link');
        $submenu->submenu_icone = mysql_result($consulta, 0, 'submenu_icone');
        $submenu->submenu_show = mysql_result($consulta, 0, 'submenu_show');
        $submenu->id_menu = mysql_result($consulta, 0, 'id_menu');
        return $submenu;
    }

    public function alterar($submenu) {
        $altera = mysql_query("UPDATE submenu SET submenu_nome='$submenu->submenu_nome', submenu_icone ='$submenu->submenu_icone', submenu_link='$submenu->submenu_link', submenu_show='$submenu->submenu_show', id_menu='$submenu->id_menu' WHERE id_submenu = '$submenu->id_submenu'");
        if ($altera == true) {
            echo "<script> alert ('Sub-Menu alterado com sucesso!') </script>";
            echo "<script> history.go (-2) </script>";
        } else {
            echo "<script> alert ('Servidor com problemas, tente mais tarde') </script>";
            echo "<script> history.go (-2) </script>";
        }
    }

    public function insere($submenu) {
        $insere = mysql_query("INSERT INTO submenu (submenu_nome, submenu_icone, submenu_link, submenu_show, id_menu) VALUES ('$submenu->submenu_nome','$submenu->submenu_icone','$submenu->submenu_link', '$submenu->submenu_show', '$submenu->id_menu')");
        if ($insere == true) {
            echo "<script> alert ('Sub-Menu inserido com sucesso!') </script>";
            echo "<script> history.go (-2) </script>";
        } else {
            echo "<script> alert ('Servidor com problemas, tente mais tarde') </script>";
            echo "<script> history.go (-2) </script>";
        }
    }

    public function excluir($submenu) {
        $exclui = mysql_query("DELETE FROM submenu WHERE id_submenu='$submenu->id_submenu'");
        if ($exclui == true) {
            echo "<script>alert('Sub-Menu excluido com sucesso!')</script>";
            echo "<script>history.go(-1)</script>";
        } else {
            echo "<script>alert('Erro! Servidor com problemas, tente mais tarde')</script>";
            echo "<script>history.go(-1)</script>";
        }
    }

}